package cn.kgc.jdbc.test;

import cn.kgc.jdbc.entity.Usr;
import cn.kgc.jdbc.util.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;

/**
 * @author mosin
 * date 2022/2/12
 * @version 1.0
 * 使用 DBUtils 工具类实现数据查询
 */
public class DBUtilsTest02 {

    public static void main(String[] args) {

        //获取数据库连接池
        DataSource dataSource = DruidUtil.getDataSource();
        // 创建 QueryRunner 的实体类对象 用于执行 数据库的增删改查操作
        QueryRunner runner = new QueryRunner(dataSource);
        String sql = "select id,username,password,birth, create_time as createTime from usr";
        try {
            List<Usr> users = runner.query(sql, new BeanListHandler<>(Usr.class));
            for (Usr user : users) {
                System.out.println("user = " + user);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
